package com.ae.shield.common.events;

import com.ae.shield.ModLib;
import com.ae.shield.common.effect.EffectList;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Set;

@Mod.EventBusSubscriber(modid = ModLib.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class EffectEvent {

    @SubscribeEvent
    public static void onEntityUpdate(LivingEvent.LivingUpdateEvent event) {
        LivingEntity entityLiving = event.getEntityLiving();
        if (entityLiving instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) entityLiving;
            if (!player.isCreative()) {
                tryFly(player);
            }
            tryAddSpeed(player);
        }
    }

    public static void onPlayerJump(LivingEvent.LivingJumpEvent event) {

    }

    /**
     * 通过实体的Tag来标记是否能够飞行
     */
    private static void tryFly(PlayerEntity player) {
        EffectInstance effect = player.getActivePotionEffect(EffectList.FLY_EFFECT.get());

        if (effect != null) {
            if (!player.getTags().contains("nanoFly_on")) {
                player.addTag("nanoFly_on");
            }
        } else if (player.getTags().contains("nanoFly_on")) {
            player.removeTag("nanoFly_on");
            player.addTag("nanoFly_off");
        }

        Set<String> tags = player.getTags();

        if (tags.contains("nanoFly_on")) {
            player.abilities.allowFlying = true;
        } else if (tags.contains("nanoFly_off")) {
            player.abilities.allowFlying = false;
            player.abilities.isFlying = false;
            player.removeTag("nanoFly_off");
        }
    }

    /**
     * 通过实体的Tag来标记是否能够跨越一格和加速
     */
    private static void tryAddSpeed(PlayerEntity player) {
        EffectInstance effect = player.getActivePotionEffect(EffectList.SPEED_EFFECT.get());

        if (effect != null) {
            if (!player.getTags().contains("nanoSpeed_on")) {
                player.addTag("nanoSpeed_on");
            }
        } else if (player.getTags().contains("nanoSpeed_on")) {
            player.removeTag("nanoSpeed_on");
            player.addTag("nanoSpeed_off");
        }

        Set<String> tags = player.getTags();

        if (tags.contains("nanoSpeed_on")) {
            if (player.isSneaking()) {
                player.stepHeight = 0.6f;
            } else {
                player.stepHeight = 1.25f;
                if (player.moveForward > 0f) {
                    if (player.isSprinting()) {
                        if (player.isSwimming()) {
                            //在游泳
                            player.moveRelative(0.03f + 0.02f * effect.getAmplifier(), new Vector3d(0d, 0d, 1d));
                        } else {
                            player.moveRelative(0.11f + 0.06f * effect.getAmplifier(), new Vector3d(0d, 0d, 1d));
                        }
                    } else {
                        if (player.isInWaterOrBubbleColumn()) {
                            //在水面
                            player.moveRelative(0.06f + 0.03f * effect.getAmplifier(), new Vector3d(0d, 0d, 1d));
                        } else {
                            player.moveRelative(0.1f, new Vector3d(0d, 0d, 1d));
                        }
                    }
                }
            }
        } else if (tags.contains("nanoSpeed_off")) {
            player.stepHeight = 0.6f;
            player.removeTag("nanoSpeed_off");
        }
    }
}
